package class02;

import java.util.Arrays;
import static class01.InsertSort.insertSort;
import static class01.SelectSort.selectSort;
import static class01.BubbleSort.bubbleSort;
/**
 * @description: 对数器
 * @time: 2021/7/5 16:37
 * @author: lhy
 */
public class CompareMachine {
    /**
     * 长度随机最大值随机的测试数组
     * @param maxLen 数组最大长度
     * @param maxValue 数组最大值
     * @return randomArr[]
     */
    public static int[] lengthValueRandomArray(int maxLen, int maxValue){
        int len = (int) (Math.random() * maxLen) + 1;
        int[] randomArr = new int[len];
        for (int i = 0; i < len; i++) {
            randomArr[i] = (int) (Math.random() * maxValue);
        }
        return randomArr;
    }

    /**
     * 判断数组是否为升序状态
     * @param arr 带测数组
     * @return true 升序 false 无序
     */
    public static boolean isSorted(int[] arr){
        if (arr.length < 2){
            return true;
        }
        int max = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < max) {
                return false;
            }
            max = Math.max(max,arr[i]);
        }
        return true;
    }


    public static void main(String[] args) {
        int maxLen = 10;
        int maxValue = 1000;
        int testTimes = 100000;
        boolean flag = true;

        for (int i = 0; i < testTimes; i++) {
            int[] arr = lengthValueRandomArray(maxLen,maxValue);
            int[] temp = Arrays.copyOf(arr,arr.length);
//            insertSort(arr);
//            selectSort(arr);
            bubbleSort(arr);
            if (!isSorted(arr)){
                flag = false;
                System.out.println("Sort is error：");
                for (int k : temp) {
                    System.out.print(k + " ");
                }
                break;
            }
        }

        if (flag) {
            System.out.println("Sort is right.");
        }
    }
}
